package io.github.wppli.config;

import java.math.BigDecimal;
import java.sql.*;
import java.util.Random;


public class DataGenerator {

    private static final String DB_URL = "jdbc:mysql://localhost:3306/chat-sociality?useUnicode=true&characterEncoding=utf8";
    private static final String DB_USER = "root";
    private static final String DB_PASSWORD = "1111";

    private static final Random random = new Random();

    public static void main(String[] args) {
        try {
            // 插入用户数据
            insertUserData(100000);
            // 插入商品数据
            insertProductData(100000);
            // 插入用户行为数据
            insertUserBehaviorData(100000);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    private static void insertUserData(int count) throws SQLException {
        String sql = "INSERT INTO user (id, social_id, username, password, email, phone, follow_count, " +
                "follower_count, favorite_count, age, bio, avatar_url, is_banned, role_type, last_login_time, " +
                "created_at, updated_at) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";

        try (Connection conn = DriverManager.getConnection(DB_URL, DB_USER, DB_PASSWORD);
             PreparedStatement pstmt = conn.prepareStatement(sql)) {

            conn.setAutoCommit(false);

            for (long i = 1; i <= count; i++) {
                pstmt.setLong(1, i);
                pstmt.setString(2, "social_" + i);
                pstmt.setString(3, "user_" + i);
                pstmt.setString(4, "password_" + i);
                pstmt.setString(5, "email_" + i + "@example.com");
                pstmt.setString(6, "1234567890");
                pstmt.setInt(7, random.nextInt(100));
                pstmt.setInt(8, random.nextInt(100));
                pstmt.setInt(9, random.nextInt(100));
                pstmt.setInt(10, random.nextInt(100));
                pstmt.setString(11, "This is a bio for user " + i);
                pstmt.setString(12, "https://example.com/avatar_" + i + ".jpg");
                pstmt.setBoolean(13, false);
                pstmt.setString(14, "USER");
                pstmt.setTimestamp(15, new Timestamp(System.currentTimeMillis()));
                pstmt.setTimestamp(16, new Timestamp(System.currentTimeMillis()));
                pstmt.setTimestamp(17, new Timestamp(System.currentTimeMillis()));

                pstmt.addBatch();

                if (i % 1000 == 0) {
                    pstmt.executeBatch();
                    conn.commit();
                    System.out.println("Inserted 1000 users, total: " + i);
                }
            }

            pstmt.executeBatch();
            conn.commit();
        }
    }

    private static void insertProductData(int count) throws SQLException {
        String sql = "INSERT INTO product (id, name, main_image, detail_images, price, market_price, " +
                "stock, category_id, brand_id, specs, description, status, is_deleted, create_time, update_time) " +
                "VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";

        try (Connection conn = DriverManager.getConnection(DB_URL, DB_USER, DB_PASSWORD);
             PreparedStatement pstmt = conn.prepareStatement(sql)) {

            conn.setAutoCommit(false);

            for (long i = 1; i <= count; i++) {
                pstmt.setLong(1, i);
                pstmt.setString(2, "Product " + i);
                pstmt.setString(3, "https://example.com/main_image_" + i + ".jpg");
                pstmt.setString(4, "[\"https://example.com/detail_image_" + i + "_1.jpg\", \"https://example.com/detail_image_" + i + "_2.jpg\"]");
                pstmt.setBigDecimal(5, new BigDecimal(random.nextDouble() * 1000).setScale(2, BigDecimal.ROUND_HALF_UP));
                pstmt.setBigDecimal(6, new BigDecimal(random.nextDouble() * 1500).setScale(2, BigDecimal.ROUND_HALF_UP));
                pstmt.setInt(7, random.nextInt(1000));
                pstmt.setInt(8, random.nextInt(100));
                pstmt.setInt(9, random.nextInt(50));
                pstmt.setString(10, "{\"spec1\": \"value1\", \"spec2\": \"value2\"}");
                pstmt.setString(11, "This is a description for product " + i);
                pstmt.setInt(12, 1);
                pstmt.setInt(13, 0);
                pstmt.setTimestamp(14, new Timestamp(System.currentTimeMillis()));
                pstmt.setTimestamp(15, new Timestamp(System.currentTimeMillis()));

                pstmt.addBatch();

                if (i % 1000 == 0) {
                    pstmt.executeBatch();
                    conn.commit();
                    System.out.println("Inserted 1000 products, total: " + i);
                }
            }

            pstmt.executeBatch();
            conn.commit();
        }
    }

    private static void insertUserBehaviorData(int count) throws SQLException {
        String sql = "INSERT INTO user_behaviors (id, user_id, product_id, behavior_type, create_time) " +
                "VALUES (?, ?, ?, ?, ?)";

        try (Connection conn = DriverManager.getConnection(DB_URL, DB_USER, DB_PASSWORD);
             PreparedStatement pstmt = conn.prepareStatement(sql)) {

            conn.setAutoCommit(false);

            for (long i = 1; i <= count; i++) {
                pstmt.setLong(1, i);
                pstmt.setLong(2, random.nextLong()); // 假设用户ID在1到100000之间
                pstmt.setLong(3, random.nextLong()); // 假设商品ID在1到100000之间
                pstmt.setString(4, getRandomBehaviorType());
                pstmt.setTimestamp(5, new Timestamp(System.currentTimeMillis() - random.nextInt(1000000000)));

                pstmt.addBatch();

                if (i % 1000 == 0) {
                    pstmt.executeBatch();
                    conn.commit();
                    System.out.println("Inserted 1000 user behaviors, total: " + i);
                }
            }

            pstmt.executeBatch();
            conn.commit();
        }
    }

    private static String getRandomBehaviorType() {
        String[] types = {"view", "collect", "add_to_cart", "purchase"};
        return types[random.nextInt(types.length)];
    }
}